package com.schoolhelper.model.entity;

import android.os.Build;

import androidx.annotation.RequiresApi;

import com.schoolhelper.model.adaptermodel.DetailRecyclerViewDiscuss;

import java.util.Date;
import java.util.List;

import lombok.*;

import static com.schoolhelper.controller.activity.DetailActivity.defaultIvUserImageUrl;

/**
 * (TDiscuss)表实体类
 *
 * @author makejava
 * @since 2021-04-23 21:03:22
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Discuss {

    // 评论id
    private Long discussId;

    // 评论的订单的id
    private Long orderId;

    // 发表评论的人的id
    private Integer userId;

    // 回复的评论id，如果为空，就是直接评论
    private Long replyDiscussId;

    // 评论正文
    private String discussText;

    // 评论的时间
    private Date discussDatetime;

    /***
     * 转为DetailRecyclerViewDiscuss类
     * @param userList 所有User信息
     * @return  DetailRecyclerViewDiscuss
     */
    @RequiresApi(api = Build.VERSION_CODES.Q)
    public synchronized DetailRecyclerViewDiscuss turn2DetailRecyclerViewDiscuss(List<User> userList){
        DetailRecyclerViewDiscuss detailRecyclerViewDiscuss = new DetailRecyclerViewDiscuss();
        detailRecyclerViewDiscuss.setDiscussDatetime(getDiscussDatetime());
        detailRecyclerViewDiscuss.setDiscussText(getDiscussText());
        detailRecyclerViewDiscuss.setReplyDiscussId(getReplyDiscussId());
        User user = null;
        for (User u : userList) {
            if (u.getUserId().equals(userId)) {
                user = u;
            }
        }

        if (user != null) {
            detailRecyclerViewDiscuss.setUsername(user.getUsername());
            String profileImgUrl = user.getProfileImg();
            if (profileImgUrl != null){
                detailRecyclerViewDiscuss.setProfileImageUrl(profileImgUrl);
            }
            else{
                detailRecyclerViewDiscuss.setProfileImageUrl(defaultIvUserImageUrl);
            }
        }
        return detailRecyclerViewDiscuss;

    }
}
